﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections;
using System.IO;

using ReadSSCInfo;
using JsRunTool.CardReader;

namespace JsRunTool
{
    public partial class Form1 : Form
    {
        static Form1 fm;
        public CardDllInterface m_card;
        public string m_jsPath;

        public Form1()
        {
            InitializeComponent();
            m_card = new CardDllInterface();
            fm = this;
            m_card.form = this;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                comboBox1.Items.Clear();
                string[] list = m_card.initReadCard();
                foreach(object o in list)
                {
                    string s = (string)o;
                    comboBox1.Items.Add(s);
                }
                comboBox1.Text = list[0];
                m_card.setReader(comboBox1.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_连接_Click(object sender, EventArgs e)
        {
            string txt = comboBox1.Text;
            m_card.setReader(comboBox1.Text);

//             testDllCallBack td = new testDllCallBack();
//             td.test();
        }

        private void btn_复位_Click(object sender, EventArgs e)
        {
            string atr = m_card.reset();
            richTextBox2.Text += "复位信息:" + atr +"\n";
        }

        private void btn_发送_Click(object sender, EventArgs e)
        {
            string apdu = txb_apdu.Text;
            string cmd = m_card.sendcmd(apdu);
            richTextBox2.Text += "返回信息:" + cmd + "\n";
        }

        private void btn_打开_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                richTextBox1.Text = System.IO.File.ReadAllText(file, System.Text.Encoding.GetEncoding("GBK"));
                m_jsPath = file;
            }
        }

        private void btn_保存_Click(object sender, EventArgs e)
        {
            richTextBox1.SaveFile(m_jsPath, RichTextBoxStreamType.PlainText);
            MessageBox.Show("保存成功!");
        }

        private void btn_运行_Click(object sender, EventArgs e)
        {
            //m_jsPath = @"E:\工作项目\工具开发\通用读卡器发卡动态库\测试脚本\补丁检测.js";
//            m_card.runJs(m_jsPath);

            ThreadParamter th = new ThreadParamter();
            th.formArgs = this;
            th.cardDll = m_card;

            MyThread demo = new MyThread(th);
            demo.Start();
        }

        public void showInfo(string txt)
        {
            richTextBox2.Text += txt + "\n";
        }
    }
}
